package main;
import java.util.ArrayList;
import jobs.*;

/*
 * A class that is used to refer to a creature
 */
public class Player extends Creature {

	//asd
	
	//variables to hold the experience values
	public int curXP;//how much experience a player currently has
	public int levelXP;//the experience required to level
	public Job job;//what a players job/class is
	
	//sets up a player
	public Player(int x, int y, String n, Job j) {
		super(x,y,n);
		super.changeOccImage("character.gif");
		curXP=0;
		levelXP=(int) Math.pow(10, level);
		level=1;
		job=j;
		strength=job.startStr;
		stamina=job.startStam;
		intelligence=job.startInt;
		spirit=job.startSp;
		maxHealth=(int)(2.4*stamina);
		curHealth=maxHealth;
		maxMana=(int)(2.4*intelligence);
		curMana=maxMana;
		weapon1=job.jobWeapon;
		armor=job.armor.armorVal;
	}
	
	//set up initial player with given starting stats
	public Player(int x, int y, String n, Job j, int str, int stam, int intel, int sp) {
		super(x,y,n);
		super.changeOccImage("character.gif");
		curXP=0;
		levelXP=(int) Math.pow(10, level);
		level=1;
		job=j;
		strength=str;
		stamina=stam;
		intelligence=intel;
		spirit=sp;
		maxHealth=(int)(1.5*stamina);
		curHealth=maxHealth;
		maxMana=(int)(1.5*intelligence);
		curMana=maxMana;
		weapon1=job.jobWeapon;
		armor=job.armor.armorVal;
	}
	
	/* Provided a basis for the combat, but is not being used currently
	public void combat(ArrayList <Creature> creatures){
    	if(!creatures.isEmpty()){
			int weaponDamage = this.weapon1.damage;
	    	double attackSpeed = this.weapon1.attackSpeed;
	    	//all becomes Character.health, Character.strength etc
	    	double timeUntilNextAct = 0;//this stays probably
	    	
	    	int eCurHealth = creatures.get(0).curHealth;
	    	int eMaxHealth = creatures.get(0).maxHealth;
	    	int eStrength = creatures.get(0).strength;
	    	int eWeaponDamage = creatures.get(0).weapon1.damage;
	    	int eLevel = creatures.get(0).level;
	    	int eArmor = creatures.get(0).armor;
	    	double eAttackSpeed = creatures.get(0).weapon1.attackSpeed;
	    	//all becomes Creature.health, Creature.strength etc
	    	double eTimeUntilNextAct = 0;//this stays probably
	    	
	    	
	    	int flag = 0;
	    	int damage = 0;
	    	
	    	while (flag == 0)
	    	{
	    		if (timeUntilNextAct <= 0) //USER ACTION
	    		{
	    			damage = attackDamage(strength,weaponDamage,level,eArmor);
		    		System.out.println("You attack enemy for " + damage + " damage.");
		    		eCurHealth = eCurHealth - damage;
		    		System.out.println("Enemy Health is now: " + eCurHealth + "/" + eMaxHealth);
		    		System.out.println();
		    		timeUntilNextAct = timeUntilNextAct + attackSpeed;
	    		}//right now only chooses you to attack. Lets user decided instead later.
	    		 //other actions can include: spells, MOVING (should take a set time, ex: 0.5)
	    		
	    		else if (eTimeUntilNextAct <= 0)//ENEMY ACTION. must make work for multi enemies later
	    		{
		    		damage = attackDamage(eStrength,eWeaponDamage,eLevel,armor);
		    		System.out.println("Enemy attacks you for " + damage + " damage.");
		    		curHealth = curHealth - damage;
		    		System.out.println("Your Health is now: " + curHealth + "/" + maxHealth);
		    		System.out.println();
		    		eTimeUntilNextAct = eTimeUntilNextAct + eAttackSpeed;
	    		}//right now only chooses to attack, enemy may use other actions later.
	   		     //other actions can include: spells, MOVING (should take a set time, ex: 0.5)
	    		 //AI: move towards player if player is not adjacent
	    		else
	    		{
	    			timeUntilNextAct = timeUntilNextAct - 0.1;
	    			eTimeUntilNextAct = eTimeUntilNextAct - 0.1;
	    		}
	    		
	    		if (curHealth <= 0)
	    		{
	    			System.out.println("You have died!");
	    			flag = 1;
	    		}
	    		else if (eCurHealth <= 0)
	    		{
	    			System.out.println("You have defeated enemy!");
	    			flag = 1;
	    		}
	    	}
    	}
	}*/
	
	public Player() {
		// TODO Auto-generated constructor stub
	}

	//give a player more xp
	public void gainXP(int x, int l){
		curXP+=x/(level-l+1);
		levelUp();
	}
	
	//checks to see if a player could level them up and levels them if they are above the experience required to level
	public void levelUp(){
		if(curXP>=levelXP){
			System.out.println("LEVEL UP");
			level++;
			levelXP=10+5*(level-1);
			job.grantSpell(this);
			strength+=job.strChange;
			stamina+=job.stamChange;
			intelligence+=job.intChange;
			spirit+=job.spChange;
			maxHealth+=(1.5*stamina);
			curHealth+=(1.5*stamina);
			maxMana+=(1.5*intelligence);
			curMana+=(1.5*intelligence);
		}//end if
	}//end method

	//calculates how much damage a player deals with their weapon
	public static int attackDamage(int aStrength,int aWeaponDamage,int aLevel, int dArmor)
    {
        double calcStrength = (double) aStrength;
        double calcDamage = calcStrength/2 + aWeaponDamage + RandNum.randNum(aLevel,aLevel*3) - dArmor;
       
        int finalDamage = (int)calcDamage;
       
        //
        double remainder = calcDamage - (int)calcDamage;
        int percentChanceUp = (int)(remainder * 100);
        int requiredUp = RandNum.randNum(1,100);
        if (percentChanceUp >= requiredUp)
        {
            finalDamage = finalDamage + 1;
        }
        //makes the damage calc account for decimal final values of damage rather than just flooring the value
        //10.4 --> 10 damage OR 11 damage, so:
        //40% of the time it deals 10 damage
        //60% of the time it deals 11 damage
        
        //System.out.print("REMAINDER IS: " + remainder + ".  ");
	    //System.out.println(finalDamage);
        return finalDamage;
    }//end method
	
	public String toString(){
		String p=name+"\n";
		p+=job+"\n"; 
		p+=curXP+"\n";
		p+=level+"\n";
		p=p+strength+"\n"+stamina+"\n"+intelligence+"\n"+spirit+"\n"+maxHealth+"\n"+maxMana+"\n";
		return p;
	}//end method
}//end class